//
//  ProductInfoTableViewController.m
//  GoodWayClient
//
//  Created by HuiDong on 15/12/25.
//  Copyright © 2015年 HuiDong. All rights reserved.
//

#import "ProductInfoTableViewController.h"
#import "ProductInfoView.h"
#import "ProductInfoFoot.h"
#define fDeviceWidth [[UIScreen mainScreen] bounds].size.width
#define fDeviceHeight [[UIScreen mainScreen] bounds].size.height

@interface ProductInfoTableViewController ()
@property (nonatomic,strong) UIScrollView *scrollView11;

@property (nonatomic, assign)NSInteger picWidth;

@property (nonatomic, assign)NSInteger picHeight;

@property (nonatomic, assign)NSInteger picNum;

@property (nonatomic, retain)NSTimer *timer;

@property (nonatomic, assign)NSInteger currentPage;
@end

@implementation ProductInfoTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    ProductInfoView *my = [ProductInfoView one];
    my.frame = CGRectMake(5, 5, fDeviceWidth-10, fDeviceHeight*0.9);
    [self.tableView setTableHeaderView:my];
    ProductInfoFoot *foot = [ProductInfoFoot one];
    foot.frame = CGRectMake(0, 0, fDeviceWidth, fDeviceHeight*0.1);
    [self.tableView setTableFooterView:foot];
    
    self.scrollView11 = my.myScrollView;
    self.scrollView11.delegate = self;
    
    self.picWidth=self.scrollView11.frame.size.width;
    self.picHeight=self.scrollView11.frame.size.height;
    self.picNum=5;
    
    for (int i=0; i<self.picNum; i++) {
        CGFloat px=i*self.picWidth;
        
        NSString *path=[NSString stringWithFormat:@"%d.jpg",i+1];
        
        UIImageView *pic=[[UIImageView alloc]initWithFrame:CGRectMake(px, 0, self.picWidth, self.picHeight)];
        
        pic.image=[UIImage imageNamed:path];
        
        [self.scrollView11 addSubview:pic];
    }
    self.scrollView11.contentSize=CGSizeMake(self.picNum*self.picWidth, 0);
    
    self.scrollView11.pagingEnabled=YES;
    
    [self startTimer];
    
    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/**
 *  开启定时器
 */
- (void)startTimer{
    self.timer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(changePic) userInfo:@"" repeats:YES];
}
/**
 *  定时器每秒触发 切换图片
 */
- (void)changePic{
    NSInteger index=self.currentPage;
    
    if (index==self.picNum-1) {
        //        self.scrollerView.contentOffset=CGPointZero;
        [self.scrollView11 setContentOffset:CGPointZero animated:YES];
        
    }
    else {
        index++;
        //self.scrollerView.contentOffset=CGPointMake(self.picWidth*index,0);
        
        [self.scrollView11 setContentOffset: CGPointMake(self.picWidth*index,0)animated:YES];//添加动画
    }
    
}
/**
 *  移除定时器
 */
- (void)removeMyTimer{
    if (self.timer!=nil) {
        [self.timer invalidate];
        self.timer=nil;
    }
}
/**
 *  滑动scorllView触发
 *
 *
 */

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat width=scrollView.contentOffset.x;
    
    self.currentPage=width/self.picWidth;
    
}

/**
 *  开始拖拽时移除定时器
 *
 *  @param scrollView
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self removeMyTimer];
}
/**
 *
 *停止拖拽时启动定时器
 */
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    [self startTimer];
}


#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    if (indexPath.row == 0) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, fDeviceWidth, fDeviceHeight*0.1)];
        label.text = @"滑动查看详情";
        label.textAlignment = 1;
        [cell.contentView addSubview:label];
    }else
    {
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, fDeviceWidth, fDeviceHeight*0.7)];
        image.image = [UIImage imageNamed:@"logo"];
        [cell.contentView addSubview:image];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return fDeviceHeight*0.1;
    }else
    {
        return fDeviceHeight*0.7;
    }
    
}




@end
